package com.smasher.update.component.download.sys

import android.app.DownloadManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.smasher.update.component.download.DownloadFinishListener

class DownloadReceiver : BroadcastReceiver() {

    private var downloadFinishListener: DownloadFinishListener? = null

    fun addFinishListener(listener: DownloadFinishListener?) {
        downloadFinishListener = listener
        Log.d(TAG, "addFinishListener: $downloadFinishListener")
    }

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == action) {
            //下载完成操作
            val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
            Log.d(TAG, "onReceive: finish id=$id")
//            downloadFinishListener?.onDownload(id)
        } else if (DownloadManager.ACTION_NOTIFICATION_CLICKED == action) {
            Log.e(TAG, "onReceive: $action")
        }
    }

    companion object {
        const val TAG = "DownloadReceiver"
    }
}
